<?php
namespace app\controllers;


use app\common\corelib\CacheHelper;
use app\common\HttpCurl;
use app\logic\GoodsLogic;
use app\common\corelib\LocalUpload;
use app\logic\PlayLogic;
use app\models\Address;
use PHPExcel_Cell_DataType;
use yii;

class GoodsController extends BackendController
{
    const LIMIT=30;

    public function actionYanxuan()
    {
        /*$httpcurl = new HttpCurl();
        $result = $httpcurl->rollpost("yanxuan.item.id.batch.get");*/
        $result = null;
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $_SESSION['page'] = $page;
        $goodsLogic = new GoodsLogic();
        $this->data['breadcrumbs'] = '严选商品库';
        return $this->render('index', [
            'local_search_first_category'=>$goodsLogic->getCategory(),
            'goods' => $goodsLogic->getGoods($result,$query, $page,self::LIMIT),
            'query' => $query
        ]);


    }

    public function actionOn(){
        $httpcurl = new HttpCurl();
        $result = $httpcurl->rollpost("yanxuan.item.id.batch.get");
        //$result = null;
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $_SESSION['page_on'] = $page;
        $goodsLogic = new GoodsLogic();
        $this->data['breadcrumbs'] = '在售商品管理';

        return $this->render('on', [
            'local_search_first_category'=>$goodsLogic->getCategory(),
            'goods' => $goodsLogic->getOnGoods($result,$query, $page,self::LIMIT),
            'query' => $query
        ]);
    }

    public function actionRecommend(){
        $goods_id = Yii::$app->request->post('goods_id');
        $goods_ishotornew = Yii::$app->request->post('goods_ishotornew');
        $goods_sort = Yii::$app->request->post('goods_sort');
        $goodsLogic = new GoodsLogic();
        $result = $goodsLogic->recommend($goods_id,$goods_ishotornew,$goods_sort);
        echo json_encode($result);
    }

    public function actionGetSecondCategory(){
        $category_name = Yii::$app->request->post('category_name');
        $second_category = CacheHelper::get("local_search_second_category");
        echo json_encode($second_category[$category_name]);
        unset($second_category);
    }

    public function actionGetSecondCategoryById(){
        $category_id = Yii::$app->request->post('category_id');
        $goodsLogic = new GoodsLogic();
        $second_category = $goodsLogic->getCategory($category_id,2);
        echo json_encode($second_category);
        unset($second_category);
    }


    public function actionSkuDetail(){
        $yx_goods_id = Yii::$app->request->get('id');
        //$onesku = CacheHelper::get('skuId'.$yx_goods_id);
        //$goods_temp = CacheHelper::get("local_goods_withspuid");
        $goodsLogic = new GoodsLogic();
        $goods = $goodsLogic->getGoodsByYxAndCate($yx_goods_id);
        $skus = $goodsLogic->getSkuByYxid($yx_goods_id);
        $categorys = $goodsLogic->getGoodsByYxAndAllCate($yx_goods_id);
        //var_dump($categorys);exit;
        /*foreach($onesku['skuList'] AS $key=>$value){
            $skuids[] = $value['id'];
        }*/
        foreach($skus AS $key=>$value){
            $skuids[] = $value['yx_sku_id'];
        }
        /*if(count($skuids)>100){
            Yii::$app->getSession()->setFlash('error','无法查看超过100个sku');
            $this->goBack(Yii::$app->request->referrer);
        }
        $skuids = json_encode($skuids);*/

        $sku_stock = [];
        $count_skuids = count($skuids);
        if($count_skuids>100){
            $page = ceil($count_skuids/100);
            for($j=1;$j<=$page;$j++) {
                $offset1 = 100 * ($j - 1);
                $skuids_temp = array_slice($skuids, $offset1, 100);
                $skuids_temp = json_encode($skuids_temp);
                $httpcurl = new HttpCurl();
                $sku_stock_temp = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids_temp]);
                $sku_stock_temp = json_decode($sku_stock_temp,true);
            }
            $sku_stock = array_merge($sku_stock,$sku_stock_temp);
        }else{
            $skuids = json_encode($skuids);
            $httpcurl = new HttpCurl();
            $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
            $sku_stock = json_decode($sku_stock,true);
        }


        /*$httpcurl = new HttpCurl();
        $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
        if(isset($sku_stock['code']) && $sku_stock['code']==20201){
            Yii::$app->getSession()->setFlash('error',$sku_stock['msg']);
            $page = $_SESSION['page'];
            jump(['goods/yanxuan','page'=>$page]);
        }
        $sku_stock = json_decode($sku_stock,true);*/
        foreach($skus AS $key=>&$value){
            foreach ($sku_stock as $v){
                if($v['skuId']==$value['yx_sku_id'])
                    $value['stock'] = $v['inventory'];
            }
        }
        //$cate = [];
        //var_dump($categorys);
        /*foreach($categorys AS $key=>$value){
            if($value['first_level'] && $value['first_level']==1){
                $cate[$value['first_category_id']]['first'] = $value['first_category_name'];
            }
            elseif($value['second_level'] && $value['second_level']==2){
                $cate[$value['parent_id']]['second'] = $value['second_category_name'];
            }
        }*/
        $goods['skuList'] = $skus;
        $goods['categorys'] = $categorys;
        //var_dump($cate);exit;
        return $this->render('sku', [
            'goodsitem' => $goods
        ]);
    }

    public function actionSkuDetailById(){
        $goods_id = Yii::$app->request->get('id');
        $goodsLogic = new GoodsLogic();
        $goods = $goodsLogic->getById($goods_id);
        $sku = $goodsLogic->getSkuById($goods_id);
        /*$skuids = array_column($sku,'yx_sku_id');
        $skuids = json_encode($skuids);
        $sku_stock = HttpCurl::post("yanxuan.inventory.count.get",['skuIds'=>$skuids]);

        $sku_stock = json_decode($sku_stock,true);*/

        foreach($sku AS $key=>&$value){
            /*foreach ($sku_stock as $v){
                if($v['skuId']==$value['yx_sku_id'])
                    $value['stock'] = $v['inventory'];
            }*/
            $value['sku_spec'] = json_decode($value['sku_spec'],true);
        }
        $goods['skuList'] = $sku;

        $goods['categoryPathList'] = $goodsLogic->getCategoryById($goods_id);
        $goods['attrList'] = json_decode($goods['attr_list'],true);


        return $this->render('on-sale-sku', [
            'goodsitem' => $goods
        ]);
    }

    public function actionOnSale(){
        if(!Yii::$app->request->post()){
            $yx_goods_id = Yii::$app->request->get('id');
            //$goods_temp = CacheHelper::get("local_goods_withspuid");
            //$onesku = CacheHelper::get('skuId'.$yx_goods_id);


            $goodsLogic = new GoodsLogic();
            $goods = $goodsLogic->getGoodsByYxAndCate($yx_goods_id);
            $skus = $goodsLogic->getSkuByYxid($yx_goods_id);
            //$categorys = $goodsLogic->getGoodsByYxAndAllCate($yx_goods_id);

            foreach($skus AS $key=>$value){
                $skuids[] = $value['yx_sku_id'];
            }
            $goodsLogic = new GoodsLogic();
            $db_goods = $goodsLogic->getGoodsByYxOne($yx_goods_id);
            if($db_goods){
                $goods['is_hot'] = $db_goods['is_hot'];
                $goods['goods_sort'] = $db_goods['goods_sort'];
            }else{
                $goods['is_hot'] = 0;
                $goods['goods_sort'] = 0;
            }

            /*if(count($skuids)>100){
                Yii::$app->getSession()->setFlash('error','无法查看超过100个sku');
                $this->goBack(Yii::$app->request->referrer);
            }
            $skuids = json_encode($skuids);
            $httpcurl = new HttpCurl();
            $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
            if(isset($sku_stock['code']) && $sku_stock['code']==20201){
                Yii::$app->getSession()->setFlash('error',$sku_stock['msg']);
                $page = $_SESSION['page'];
                jump(['goods/yanxuan','page'=>$page]);
            }
            $sku_stock = json_decode($sku_stock,true);*/

            $sku_stock = [];
            $count_skuids = count($skuids);
            if($count_skuids>100){
                $page = ceil($count_skuids/100);
                for($j=1;$j<=$page;$j++) {
                    $offset1 = 100 * ($j - 1);
                    $skuids_temp = array_slice($skuids, $offset1, 100);
                    $skuids_temp = json_encode($skuids_temp);
                    $httpcurl = new HttpCurl();
                    $sku_stock_temp = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids_temp]);
                    $sku_stock_temp = json_decode($sku_stock_temp,true);
                }
                $sku_stock = array_merge($sku_stock,$sku_stock_temp);
            }else{
                $skuids = json_encode($skuids);
                $httpcurl = new HttpCurl();
                $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
                $sku_stock = json_decode($sku_stock,true);
            }

            foreach($skus AS $key=>&$value){
                foreach ($sku_stock as $v){
                    if($v['skuId']==$value['yx_sku_id'])
                        $value['stock'] = $v['inventory'];
                }
            }
            $goods['skuList'] = $skus;
            return $this->render('on-sale', [
                'goodsitem' => $goods
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $yx_goods_id = $params['yx_goods_id'];
            //$goods_temp = CacheHelper::get("local_goods_withspuid");
            //$onesku = CacheHelper::get('skuId'.$yx_goods_id);
            $goodsLogic = new GoodsLogic();
            $fin_result = true;
            /*$status_goods = CacheHelper::get("local_goods_withstatus");
            if(!isset($status_goods[1]))
                $status_goods[1] = [];
            if(!isset($status_goods[0]))
                $status_goods[0] = [];
            if(!isset($status_goods[2]))
                $status_goods[2] = [];*/
            //$haveone = $goodsLogic->getByYxId($yx_goods_id);

            /*$skuids = [];
            foreach($onesku['skuList'] AS $key=>$value){
                $skuids[] = $value['id'];
            }*/
            /*$skuids = json_encode($skuids);
            $httpcurl = new HttpCurl();
            $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
            if(isset($sku_stock['code']) && $sku_stock['code']==20201){
                Yii::$app->getSession()->setFlash('error',$sku_stock['msg']);
                $page = $_SESSION['page'];
                jump(['goods/yanxuan','page'=>$page]);
            }
            $sku_stock = json_decode($sku_stock,true);*/


            /*$sku_stock = [];
            $count_skuids = count($skuids);
            if($count_skuids>100){
                $page = ceil($count_skuids/100);
                for($j=1;$j<=$page;$j++) {
                    $offset1 = 100 * ($j - 1);
                    $skuids_temp = array_slice($skuids, $offset1, 100);
                    $skuids_temp = json_encode($skuids_temp);
                    $httpcurl = new HttpCurl();
                    $sku_stock_temp = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids_temp]);
                    $sku_stock_temp = json_decode($sku_stock_temp,true);
                }
                $sku_stock = array_merge($sku_stock,$sku_stock_temp);
            }else{
                $skuids = json_encode($skuids);
                $httpcurl = new HttpCurl();
                $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
                $sku_stock = json_decode($sku_stock,true);
            }*/

            /*if(!$haveone){
                //$fin_result &= $goodsLogic->addGoods($onesku,$params,$sku_stock);

            }else{*/
                $status = 1;
                $fin_result &= $goodsLogic->saveStatusByYx($yx_goods_id,$status,$params);
            //}
            /*if(!in_array($goods_temp[$yx_goods_id],$status_goods[1]))
                array_push($status_goods[1],$goods_temp[$yx_goods_id]);*/
            //unset($status_goods);
            //unset($onesku);
            if($fin_result){
                /*foreach($status_goods[2] AS $k=>$v){
                    if($v['id']==$yx_goods_id){
                        array_splice($status_goods[2],$k,1);
                    }
                }
                foreach($status_goods[0] AS $ke=>$va){
                    if($va['id']==$yx_goods_id){
                        array_splice($status_goods[0],$ke,1);
                    }
                }*/

                $page = $_SESSION['page'];
                Yii::$app->getSession()->setFlash('success','上架成功');
                jump(['goods/yanxuan','page'=>$page]);
            }else{
                $page = $_SESSION['page'];
                Yii::$app->getSession()->setFlash('error','该商品网易已下架，不可上架');
                jump(['goods/yanxuan','page'=>$page]);
            }

        }
    }



    public function actionBatchOn(){
        if(!Yii::$app->request->post()){
            $yx_goods_ids = Yii::$app->request->get('yx_goods_ids');
            $arr = explode(',',trim($yx_goods_ids,','));
            //$goods_temp = CacheHelper::get("local_goods_withspuid");

            $goodsLogic = new GoodsLogic();
            $db_goods = $goodsLogic->getGoodsByYx($arr);
            foreach ($arr as $item) {
                //$goods_temp[$item] = CacheHelper::get('skuId'.$item);


                foreach($db_goods AS $kk=>$vv){
                    if($vv['yx_goods_id']==$item){
                        $goods_temp[$item] = $vv;
                        $goods_temp[$item]['is_hot'] = $vv['is_hot'];
                        $goods_temp[$item]['goods_sort'] = $vv['goods_sort'];
                        break;
                    }else{
                        $goods_temp[$item]['is_hot'] = 0;
                        $goods_temp[$item]['goods_sort'] = 0;
                    }
                }
                $skus = $goodsLogic->getSkuByYxid($item);
                $goods_temp[$item]['skuList'] = $skus;
                foreach($skus AS $key=>$value){
                    $skuids[] = $value['yx_sku_id'];
                }
            }
            if(count($skuids)>100){
                Yii::$app->getSession()->setFlash('error','无法批量上架超过100个sku');
                $this->goBack(Yii::$app->request->referrer);
            }

            /*$skuids = json_encode($skuids);
            $httpcurl = new HttpCurl();
            $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
            if(isset($sku_stock['code']) && $sku_stock['code']==20201){
                Yii::$app->getSession()->setFlash('error',$sku_stock['msg']);
                $page = $_SESSION['page'];
                jump(['goods/yanxuan','page'=>$page]);
            }
            $sku_stock = json_decode($sku_stock,true);*/

            $sku_stock = [];
            $count_skuids = count($skuids);
            if($count_skuids>100){
                $page = ceil($count_skuids/100);
                for($j=1;$j<=$page;$j++) {
                    $offset1 = 100 * ($j - 1);
                    $skuids_temp = array_slice($skuids, $offset1, 100);
                    $skuids_temp = json_encode($skuids_temp);
                    $httpcurl = new HttpCurl();
                    $sku_stock_temp = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids_temp]);
                    $sku_stock_temp = json_decode($sku_stock_temp,true);
                }
                $sku_stock = array_merge($sku_stock,$sku_stock_temp);
            }else{
                $skuids = json_encode($skuids);
                $httpcurl = new HttpCurl();
                $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
                $sku_stock = json_decode($sku_stock,true);
            }

            foreach ($arr as $item) {
                foreach($goods_temp[$item]['skuList'] AS $key=>&$value){
                    foreach ($sku_stock as $v){
                        if($v['skuId']==$value['yx_sku_id'])
                            $value['stock'] = $v['inventory'];
                    }
                }
                $result[] = $goods_temp[$item];
            }
            unset($goods_temp);
            return $this->render('batch-on', [
                'goods' => $result
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $yx_goods_ids = $params['yx_goods_id'];
            //$goods_temp = CacheHelper::get("local_goods_withspuid");
            $goodsLogic = new GoodsLogic();
            $fin_result = true;
            /*$status_goods = CacheHelper::get("local_goods_withstatus");
            if(!isset($status_goods[1]))
                $status_goods[1] = [];
            if(!isset($status_goods[0]))
                $status_goods[0] = [];
            if(!isset($status_goods[2]))
                $status_goods[2] = [];*/

            /*$skuids =[];
            foreach ($yx_goods_ids as $item1) {
                $goods_temp[$item1] = CacheHelper::get('skuId'.$item1);
                foreach($goods_temp[$item1]['skuList'] AS $key=>$value){
                    $skuids[] = $value['id'];
                }
            }*/
            /*$skuids = json_encode($skuids);
            $httpcurl = new HttpCurl();
            $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
            if(isset($sku_stock['code']) && $sku_stock['code']==20201){
                Yii::$app->getSession()->setFlash('error',$sku_stock['msg']);
                $page = $_SESSION['page'];
                jump(['goods/yanxuan','page'=>$page]);
            }
            $sku_stock = json_decode($sku_stock,true);*/

            /*$sku_stock = [];
            $count_skuids = count($skuids);
            if($count_skuids>100){
                $page = ceil($count_skuids/100);
                for($j=1;$j<=$page;$j++) {
                    $offset1 = 100 * ($j - 1);
                    $skuids_temp = array_slice($skuids, $offset1, 100);
                    $skuids_temp = json_encode($skuids_temp);
                    $httpcurl = new HttpCurl();
                    $sku_stock_temp = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids_temp]);
                    $sku_stock_temp = json_decode($sku_stock_temp,true);
                }
                $sku_stock = array_merge($sku_stock,$sku_stock_temp);
            }else{
                $skuids = json_encode($skuids);
                $httpcurl = new HttpCurl();
                $sku_stock = $httpcurl->rollpost("yanxuan.inventory.count.get",['skuIds'=>$skuids]);
                $sku_stock = json_decode($sku_stock,true);
            }*/

            foreach ($yx_goods_ids as $item) {
                /*$haveone = $goodsLogic->getByYxId($item);
                if(!$haveone){
                    $fin_result &= $goodsLogic->addGoods($goods_temp[$item], $params,$sku_stock);
                }else{*/
                    $status = 1;
                    $fin_result &= $goodsLogic->saveStatusByYx($item,$status,$params);
                //}
                /*if (!in_array($goods_temp[$item], $status_goods[1]))
                    array_push($status_goods[1], $goods_temp[$item]);*/

            }
            //unset($goods_temp);
            //unset($status_goods);
            if($fin_result){
                /*foreach($status_goods[0] AS $ke=>$va){
                    if(in_array($va['id'],$yx_goods_ids)){
                        array_splice($status_goods[0],$ke,1);
                    }
                }
                foreach($status_goods[2] AS $k=>$v){
                    if(in_array($v['id'],$yx_goods_ids)){
                        array_splice($status_goods[2],$k,1);
                    }
                }*/
                $page = $_SESSION['page'];
                Yii::$app->getSession()->setFlash('success','批量上架成功');
                jump(['goods/yanxuan','page'=>$page]);
            }else{
                $page = $_SESSION['page'];
                Yii::$app->getSession()->setFlash('error','部分商品网易已下架，不可上架');
                jump(['goods/yanxuan','page'=>$page]);
            }

        }
    }

    public function actionOff(){
        $id = Yii::$app->request->get('id');
        $goodsLogic = new GoodsLogic();
        //$goods = $goodsLogic->getById($id);
        $logicRes = $goodsLogic->off($id);
        /*$status_goods = CacheHelper::get("local_goods_withstatus");
        $goods_temp = CacheHelper::get("local_goods_withspuid");
        if(!isset($status_goods[1]))
            $status_goods[1] = [];
        if(!isset($status_goods[0]))
            $status_goods[0] = [];
        if(!isset($status_goods[2]))
            $status_goods[2] = [];*/
        if ($logicRes) {
            /*if(!in_array($goods_temp[$goods['yx_goods_id']],$status_goods[2]))
                array_push($status_goods[2],$goods_temp[$goods['yx_goods_id']]);
            foreach($status_goods[1] AS $ke=>$va){
                if($va['id']==$goods['yx_goods_id']){
                    array_splice($status_goods[1],$ke,1);
                }
            }*/
            Yii::$app->getSession()->setFlash('success','下架成功');
        } else {
            Yii::$app->getSession()->setFlash('error','下架失败');
        }
        $page = $_SESSION['page_on'];
        //return $this->redirect(U('goods/on','page'=>$page));
        jump(['goods/on','page'=>$page]);
    }

    public function actionBatchOff(){
        $goods_ids = Yii::$app->request->post('goods_ids');
        $arr = explode(',',$goods_ids);
        $goodsLogic = new GoodsLogic();
        $logicRes = true;
        foreach ($arr AS $value){
            $logicRes &= $goodsLogic->off($value);
        }
        if ($logicRes) {
            echo json_encode(['status'=>1]);
        } else {
            echo json_encode(['status'=>0]);
        }
    }

    /*public function actionOnSale(){
        $yx_goods_ids = Yii::$app->request->post('yx_goods_ids');
        $arr = explode(',',$yx_goods_ids);
        $goods_temp = CacheHelper::get("local_goods_withspuid");
        $goodsLogic = new GoodsLogic();
        $fin_result = true;
        $status_goods = CacheHelper::get("local_goods_withstatus");
        foreach ($arr as $item) {
            $haveone = $goodsLogic->getByYxId($item);
            if(!$haveone){
                $fin_result &= $goodsLogic->addGoods($goods_temp[$item]);
            }else{
                $status = 1;
                $fin_result &= $goodsLogic->saveStatus($goods_temp[$item],$status);
            }
            array_push($status_goods[1],$goods_temp[$item]);
        }
        if($fin_result){
            foreach($status_goods[2] AS $k=>$v){
                if(in_array($v['id'],$arr)){
                    array_splice($status_goods[2],$k,1);
                }
            }
            foreach($status_goods[0] AS $ke=>$va){
                if(in_array($va['id'],$arr)){
                    array_splice($status_goods[0],$ke,1);
                }
            }

        }
    }*/

    public function actionOffSale(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $goodsLogic = new GoodsLogic();
        $this->data['breadcrumbs'] = '下架商品管理';

        return $this->render('off', [
            'local_search_first_category'=>$goodsLogic->getCategory(),
            'goods' => $goodsLogic->getOffGoods($query, $page),
            'query' => $query
        ]);
    }

    public function actionOffSkuDetailById(){
        $goods_id = Yii::$app->request->get('id');
        $goodsLogic = new GoodsLogic();
        $goods = $goodsLogic->getOffById($goods_id);
        $sku = $goodsLogic->getOffSkuById($goods_id);

        foreach($sku AS $key=>&$value){
            $value['sku_spec'] = json_decode($value['sku_spec'],true);
        }
        $goods['skuList'] = $sku;

        $goods['categoryPathList'] = $goodsLogic->getOffCategoryById($goods_id);
        $goods['attrList'] = json_decode($goods['attr_list'],true);


        return $this->render('off-sale-sku', [
            'goodsitem' => $goods
        ]);
    }

    public function actionDelOff(){
        $goods_id = Yii::$app->request->get('id');
        $goodsLogic = new GoodsLogic();
        $logicRes = $goodsLogic->delOffById($goods_id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        return $this->redirect(U('goods/off-sale'));
    }


    public function actionOffRecommend(){
        $id = Yii::$app->request->get('id');
        $goodsLogic = new GoodsLogic();
        $logicRes = $goodsLogic->offRecommend($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','取消推荐成功');
        } else {
            Yii::$app->getSession()->setFlash('error','取消推荐失败');
        }
        $page = $_SESSION['page_on'];
        jump(['goods/on','page'=>$page]);
    }


    public function actionGoodsSku(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $goodsLogic = new GoodsLogic();
        $this->data['breadcrumbs'] = '价格管理';
        return $this->render('goodssku', [
            'local_search_first_category'=>$goodsLogic->getCategory(),
            'goodssku' => $goodsLogic->getOnGoodsSku($query, $page,100),
            'query' => $query
        ]);
    }

    public function actionChangePrice(){
        if(!Yii::$app->request->post()){
            $query = Yii::$app->request->get('form', []);
            $page = Yii::$app->request->get('page', 1);
            $goodsLogic = new GoodsLogic();
            $audit = $goodsLogic->getPriceAudit($query, $page,self::LIMIT);
            return $this->render('audit', [
                'theme' => '/assets/',
                'audit' => $audit,
                'query' => $query
            ]);
        }else{
            $skuids = Yii::$app->request->post('skuids');
            $price_type = Yii::$app->request->post('price_type');
            $price_base = Yii::$app->request->post('price_base');
            $price_model = Yii::$app->request->post('price_model');
            $price_count = Yii::$app->request->post('price_count');
            $goodsLogic = new GoodsLogic();
            $goodsLogic->addAudit($skuids,$price_type,$price_base,$price_model,$price_count);
            $audit = $goodsLogic->getPriceAudit([], 1,self::LIMIT);
            return $this->render('audit', [
                'theme' => '/assets/',
                'audit' => $audit,
                'query' => []
            ]);
        }
    }

    public function actionAudit(){
        $audit_id = Yii::$app->request->get('id');
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $goodsLogic = new GoodsLogic();
        $this->data['breadcrumbs'] = '审核';
        return $this->render('goodssku-audit', [
            'audit_id'=>$audit_id,
            'local_search_first_category'=>$goodsLogic->getCategory(),
            'goodssku' => $goodsLogic->getAuditGoodsSku($audit_id,$query, $page,100),
            'query' => $query
        ]);
    }

    public function actionExportAudit(){
        $audit_id = Yii::$app->request->get('id');
        $goodsLogic = new GoodsLogic();
        $gooodssku = $goodsLogic->getExportAuditGoodsSku($audit_id);
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator("中经汇通")
            ->setTitle("产品价格清单");
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'SPUID')
            ->setCellValue('B1', 'SKUID')
            ->setCellValue('C1', '商品名称')
            ->setCellValue('D1', '一级类目')
            ->setCellValue('E1', '二级类目')
            ->setCellValue('F1', '严选价格')
            ->setCellValue('G1', '合约价格')
            ->setCellValue('H1', '修改前价格')
            ->setCellValue('I1', '修改后价格');
        foreach($gooodssku AS $ke=>$va){
            $keys = $ke+2;
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValueExplicit('A'.$keys, $va['yx_goods_id'],PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('B'.$keys, $va['yx_sku_id'],PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue('C'.$keys, $va['goods_name'])
                ->setCellValue('D'.$keys, isset($va['first_category_name'])?$va['first_category_name']:"")
                ->setCellValue('E'.$keys, isset($va['second_category_name'])?$va['second_category_name']:"")
                ->setCellValue('F'.$keys, $va['yanxuan_price'])
                ->setCellValue('G'.$keys, $va['channel_price'])
                ->setCellValue('H'.$keys, $va['sku_price'])
                ->setCellValue('I'.$keys, $va['temp_price']);
        }
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        header('pragma:public');
        header("Content-Disposition:attachment;filename=产品价格清单".date('_YmdHis').".xlsx");
        $objWriter->save('php://output');exit;
    }

    public function actionAuditNopass(){
        $audit_id = Yii::$app->request->get('id');
        $goodsLogic = new GoodsLogic();
        $logicRes = $goodsLogic->nopass($audit_id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','审核不通过成功');
        } else {
            Yii::$app->getSession()->setFlash('error','审核不通过失败');
        }
        return $this->redirect(U('goods/change-price'));
    }

    public function actionAuditPass(){
        $audit_id = Yii::$app->request->get('id');
        $goodsLogic = new GoodsLogic();
        $logicRes = $goodsLogic->pass($audit_id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','审核通过成功');
        } else {
            Yii::$app->getSession()->setFlash('error','审核通过失败');
        }
        return $this->redirect(U('goods/change-price'));
    }


}
